package com.lry.crawlers.video.pipeline;

import com.lry.crawlers.message.entity.Message;
import com.lry.crawlers.message.service.MessageService;
import com.lry.crawlers.video.entity.Video;
import com.lry.crawlers.video.entity.VideoChapter;
import com.lry.crawlers.video.entity.VideoUpdate;
import com.lry.crawlers.video.service.VideoChapterService;
import com.lry.crawlers.video.service.VideoService;
import com.lry.crawlers.video.service.VideoUpdateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Component
public class VideoPipeline implements Pipeline {
    @Autowired
    VideoService videoService;
    @Autowired
    VideoChapterService videoChapterService;
    @Autowired
    VideoUpdateService videoUpdateService;
    @Autowired
    MessageService messageService;




    public void process(ResultItems resultItems, Task task) {
        //获取数据
        Map<String,Object> map= resultItems.get("data");

        //判断数据是否不为空
        if(map!= null) {
            //判断数据类型
            if (map.get("key").equals("videoChapter")){
                int z= chapter(map);
                if(z==10){
                    video(map);
                }
            }else if(map.get("key").equals("chapter")){
                chapter(map);
            }



        }
    }
    //处理视频搜索i
    public void video(Map<String,Object> map) {
        Video video = null;
        String title = (String)  map.get("title");
        String desc = (String)  map.get("desc");
        String author = (String)  map.get("author");
        String videoType =(String)  map.get("videoType");
        String cover = (String)  map.get("cover");
        String updateTime = (String)  map.get("updateTime");
        String videoId = (String) map.get("videoId");

        Integer id = (Integer) map.get("id");
        video = new Video();
        video.setTitle(title.trim());
        video.setAuthor(author.trim());
        video.setCreationTime(new Date());
        video.setDescs(desc.trim());
        video.setVideoId(id.toString()+videoId);
        video.setVideoType(videoType.trim());
        video.setCover(cover);
        video.setVideoVariableId(id);
        video.setUpdateTime(updateTime);

        try {
            //添加到mysql
            int i= videoService.setVideo(video);
            if(i==0){
                messageService.setMessage(new Message("mysql视频搜索阶段入库失败,videoId:"+id.toString()+videoId,new Date()));
            }
        }catch (DuplicateKeyException e){
        }catch (Exception e){
            messageService.setMessage(new Message("视频搜索阶段入库失败,videoId:"+id.toString()+videoId+" .Extends："+e,new Date()));
        }
    }


    //处理视频章节
    public int chapter(Map<String,Object> map){
        int z = 0;
        VideoChapter videoChapter = null;
        List<String> chapterTitle = (List<String>) map.get("chapterTitle");
        List<String> chapterUrl = (List<String>) map.get("chapterUrl");
        String videoId = (String) map.get("videoId");
        Integer xsize = (Integer) map.get("chapterNum");
        Integer id = (Integer) map.get("id");
        List<VideoChapter> videoChapters = new ArrayList<>();
        String updateUrl = (String) map.get("updateUrl");
        updateUrl = "/"+updateUrl.split("/", 4)[3];
        for (int i =0;i<chapterTitle.size();i+=1) {
            videoChapter = new VideoChapter();
            videoChapter.setChapterUrl(chapterUrl.get(i).trim());
            videoChapter.setVideoId(id.toString()+videoId);
            videoChapter.setTitle(chapterTitle.get(i).trim());
            videoChapter.setCreationTime(new Date());
            videoChapter.setVideoVariableId(id);
            videoChapters.add(videoChapter);
        }

        try {
            //添加到mysql
            int i  = videoChapterService.setVideoChapters(videoChapters);
            z = 10;
            if(i==0){
                messageService.setMessage(new Message("mysql视频章节阶段入库失败,videoId:"+id.toString()+videoId,new Date()));
            }else{
                int upi = 0;
                if(xsize!=null){
                    videoService.setupdateTime(new Video(id.toString()+videoId,(String) map.get("updateTime")));

                    upi = videoUpdateService.setVideoUpdateUp(new VideoUpdate(id.toString()+videoId,updateUrl,xsize,id,(String) map.get("updateTime")));
                }else {
                    upi = videoUpdateService.setVideoUpdate(new VideoUpdate(id.toString()+videoId,updateUrl, videoChapters.size(),id,(String) map.get("updateTime")));
                }

                if(upi==0){
                    messageService.setMessage(new Message("视频章节更新表入库失败,videoId:"+id.toString()+videoId,new Date()));
                }
            }
        }catch (DuplicateKeyException e){
        }catch (Exception e){
            messageService.setMessage(new Message("视频章节阶段入库失败,videoId:"+id.toString()+videoId+" .Extends："+e,new Date()));
        }
        return z;
    }



    }

